#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <linux/videodev.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pthread.h>
#include <fcntl.h>
#include <time.h>

#include "../../capture.h"

static pthread_t worker;

//extern GLOBALS global;
GLOBALS *pGlobal;

static int  delay;
static unsigned char *frame=NULL;
static char *file;

static void *worker_thread( void *arg ) {
    int  frame_size=0;

    if ( (frame = (unsigned char *)malloc(512*1024)) == NULL ) {
        perror("not enough memory for worker thread\n");
        exit(EXIT_FAILURE);
    }
    
     if(access("/tmp/capture",0)==-1)//access函数是查看文件是不是存在
    {
        if (mkdir("/tmp/capture",S_IRWXU | S_IRWXG | S_IRWXO))//如果不存在就用mkdir函数来创建
        {
            printf("creat file bag failed!!!");
        }
    }
    unsigned int i = 0;
    char *filePath = "/tmp/capture/test";
    
     if ( ( file = (char *) malloc (1024) ) == NULL ) {
        perror("not enough memory for worker thread\n");
        exit(EXIT_FAILURE);
    }
    
    memset(file, 0, 1024);
    char *hz = ".jpg";
  
    while ( !pGlobal->stopFlag ) {
        //DBG("waiting for fresh frame\n");
        pthread_mutex_lock(&pGlobal->db);
        pthread_cond_wait(&pGlobal->db_update, &pGlobal->db);

        /* read buffer */
        frame_size = pGlobal->bufSize;
        memcpy(frame, pGlobal->buf, frame_size);

        pthread_mutex_unlock( &pGlobal->db );
    
        fprintf(stderr, "\tstart write to file.............\n");
        sprintf(file, "%s%d%s", filePath, i++, hz);
        int dd;
        if( (dd = open(file, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0 ) 
        {	
            fprintf(stderr, "打开文件:  %s  失败!\n", file);
            perror("can't open file");
            exit(EXIT_FAILURE);
            return NULL;
        }
        write(dd, frame, frame_size);
        close(dd);
        fprintf(stderr, "\tcomplete write to file.............\n\n");
      
        if (delay > 0) {
//            usleep(1000*delay);
        }
        //fprintf(stderr, "output worker\n");
    }
    //fprintf(stderr, "output worker\n");
    return ( (void *) 0);
}


int output_init(char *outputParam, void *pglobal) {
  pGlobal = (GLOBALS*) pglobal;
      if(pGlobal->avi_mode)
      {
	fprintf(stderr, "can't use output file mode with use output avi mode!!\n");
	exit(EXIT_FAILURE);
      }
  delay = 10;  
    //fprintf(stderr, "output_init\n");
    return 0;
}

int output_stop(void) {
    //pthread_cancel(worker);
  //等待线程退出
    int terr;
    void * tret;
    terr = pthread_join(worker, &tret);
    if(terr != 0)
    {
      perror("join thread input_yuyv worker_thread error!");
      exit(EXIT_FAILURE);
    }
    
    free(frame);
    free(file);
    return 0;
}


int output_run(void) {
    //fprintf(stderr,"output_run\n");
    pthread_create(&worker, 0, worker_thread, NULL);
    
    //使用了 pthread_join， 固不再使用线程分离
    //pthread_detach(worker);
    return 0;
}
